package com.yiyi.credit.manage.abutment.manage.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yiyi.credit.manage.abutment.client.UserAuthorityManagementClient;
import com.yiyi.credit.manage.abutment.common.bean.Constant;
import com.yiyi.credit.manage.abutment.common.bean.Response;
import com.yiyi.credit.manage.abutment.common.enums.ResponseCode;
import com.yiyi.credit.manage.abutment.common.exceptions.ServiceException;
import com.yiyi.credit.manage.abutment.service.SubStationService;
import com.yiyi.credit.manage.model.entity.SubStation;
import com.yiyi.credit.manage.model.po.SubStationAddPo;
import com.yiyi.credit.manage.model.po.SubStationQueryPo;
import com.yiyi.credit.manage.model.po.SubstationTradePO;
import com.yiyi.credit.manage.model.vo.SubstationTradeVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping(value = "sseSubStation")
public class SubStationController {

    @Autowired
    SubStationService subStationService;

    @Autowired
    UserAuthorityManagementClient userAuthorityManagementClient;


    @ApiOperation(value = "站点列表", notes = "站点所以列表", response = SubStation.class)
    @RequestMapping(value = "/getSubStationList", method = RequestMethod.POST)
    public Response<PageInfo<SubStation>> getSubStationList() throws ServiceException {
        List<SubStation> result = subStationService.getSubStationList();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(result);
        return new Response<>(pageInfo);
    }


    @ApiOperation(value = "权限系统获取站点列表", notes = "权限系统获取站点列表", response = SubStation.class)
    @RequestMapping(value = "/ssoGetSubStationList", method = RequestMethod.POST)
    public Object ssoGetSubStationList() throws ServiceException {
        List<SubStation> result = subStationService.getSubStationList();
        return new Response<>(result);
    }


    @ApiOperation(value = "站点分页列表查询", notes = "根据不同的条件查询站点分页列表", response = SubStation.class)
    @RequestMapping(value = "/getSubStationPageList", method = RequestMethod.POST)
    public Object getSubStationPageList(@ModelAttribute SubStationQueryPo param) throws ServiceException {
        PageHelper.startPage(param.getPageIndex(), param.getPageSize());
        SubStation subStation = new SubStation();
        BeanUtils.copyProperties(param, subStation);

        PageInfo<SubStation> pageInfo = new PageInfo<SubStation>(subStationService.getSubStationPageList(subStation));
        return new Response<>(pageInfo);

    }


    @ApiOperation(value = "新增站点", notes = "新增站点", response = Response.class)
    @RequestMapping(value = "/addSubStation", method = RequestMethod.POST)
    public Object addSubStation(@ModelAttribute SubStationAddPo subStationAddPo) throws ServiceException {
        Response<SubStation> result = new Response<>("新增站点成功!");
        SubStation subStation = new SubStation();
        subStation.setSubStationNo(subStationAddPo.getSubStationNo());
        subStation.setSubStationName(subStationAddPo.getSubStationName());
        subStation.setChargePeople(subStationAddPo.getChargePeople());
        subStation.setContactPhone(subStationAddPo.getContactPhone());
        subStation.setCreateTime(new Date().getTime());
        subStation.setStatus(Constant.SUB_STATION_DISABLE);
        subStation.setCreatePeople(50L);
        subStationService.addSubStation(subStation);

        return result;
    }


    @ApiOperation(value = "修改站点", notes = "修改站点", response = Response.class)
    @RequestMapping(value = "/updateSubStation", method = RequestMethod.POST)
    public Object updateSubStation(@ModelAttribute SubStationAddPo subStationAddPo) throws ServiceException {
        Response<SubStation> result = new Response<>("修改站点成功!");

        SubStation subStation = new SubStation();
        subStation.setId(subStationAddPo.getSubStationId());
        subStation.setSubStationNo(subStationAddPo.getSubStationNo());
        subStation.setSubStationName(subStationAddPo.getSubStationName());
        subStation.setChargePeople(subStationAddPo.getChargePeople());
        subStation.setContactPhone(subStationAddPo.getContactPhone());
        subStation.setCreateTime(new Date().getTime());
        subStation.setStatus(Constant.SUB_STATION_DISABLE);
        subStation.setCreatePeople(50L);
        subStationService.updateSubStation(subStation);

        return result;
    }


    @ApiOperation(value = "删除站点", notes = "删除站点", response = Response.class)
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long", paramType = "query"),
            })
    @RequestMapping(value = "/deleteSubStation", method = RequestMethod.POST)
    public Object deleteSubStation(SubStation subStation) throws ServiceException {
        Response<SubStation> result = new Response<>("删除站点成功!");

        subStationService.deleteSubStation(subStation);

        return result;
    }


    @ApiOperation(value = "站点状态", notes = "站点状态", response = Response.class)
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "Long", paramType = "query"),
            })
    @RequestMapping(value = "/changeSubStationStatus", method = RequestMethod.POST)
    public Object changeSubStationStatus(SubStation subStation) throws ServiceException {
        Response<SubStation> result = new Response<>("站点状态修改成功!");

        subStationService.changeSubStationStatus(subStation);

        return result;
    }


    @ApiOperation(value = "查询站点", notes = "查询站点", response = SubStation.class)
    @RequestMapping(value = "/getSubStationById", method = RequestMethod.POST)
    public Object getSubStationById(String ids) throws ServiceException {
        Response<List<SubStation>> result = null;
        List<SubStation> subStationList = subStationService.getSubStationByIds(ids);
        result = new Response<>(subStationList);
        return result;
    }


    @ApiOperation(value = "站点编号是否存在", notes = "站点编号是否存在", response = SubStation.class)
    @RequestMapping(value = "/checkSubStationNo", method = RequestMethod.POST)
    public Object checkSubStationNo(String subStationNo) throws ServiceException {

        int size = subStationService.checkSubStationNo(subStationNo);
        if (size == 0) {
            return new Response<>(ResponseCode.SUCCESS);
        }
        return new Response<>(ResponseCode.EXIST_SUB_STATION_NO);
    }

    @ApiOperation(value = "站点交易查询", notes = "查询站点交易")
    @RequestMapping(value = "/tradeList", method = {RequestMethod.GET, RequestMethod.POST})
    public Response<PageInfo<SubstationTradeVO>> substationTrade(@ModelAttribute SubstationTradePO tradePO) {
        return new Response<>(new PageInfo<>(subStationService.listTrades(tradePO)));
    }

}
